#include <iostream>

#include "mainWindow.hpp"
#include "response.hpp"
#include "recieve_t.hpp"
#include "send_t.hpp"

mainWindow::mainWindow(QWidget *parent, QString yn)
	: QWidget(parent)
{
	///Creating Downloads Window
	downloads = new QPushButton("Downloads");
	send_file = new QPushButton("Send file");
	scroll_area = new QScrollArea;
	scroll_widget = new QWidget;
	file_browser = new QFileDialog;

	scroll_area->setWindowTitle("Downloads");
	progress_layout = new QVBoxLayout(scroll_widget);
	scroll_widget->setLayout(progress_layout);
	scroll_area->resize(500,200);
	QSpacerItem *spacer = new QSpacerItem(0, 0, 
			QSizePolicy::Minimum, QSizePolicy::Expanding);
	progress_layout->addItem(spacer);

	fd = -2;
        resize(238,354);
	setWindowTitle("Armessenger");
	hostNick = yn;
	uName = new QLabel(yn);
	uName->setTextFormat(Qt::AutoText);
	uName->setAlignment(Qt::AlignCenter);

	addUser = new QPushButton(tr("add friend"));
	contacts = new QListWidget;

	connect(addUser, SIGNAL(clicked()), this, SLOT(addContact()));

	QHBoxLayout *button_layout = new QHBoxLayout;
	button_layout->addWidget(addUser);
	button_layout->addWidget(send_file);
	button_layout->addWidget(downloads);

	layout =  new QVBoxLayout;
	layout->addWidget(uName);
	layout->addWidget(contacts);
	layout->addLayout(button_layout);
	setLayout(layout);

	connect(this->contacts, SIGNAL(itemDoubleClicked(QListWidgetItem*)), 
			this, SLOT(chattingWindow(QListWidgetItem*)));
	connect(this, SIGNAL(incomingConnection()),this, SLOT(addToList()));
	connect(this, SIGNAL(file_request(int,char*,int)),
				this, SLOT(response_file_request(int, char*,int)));
	connect(downloads,SIGNAL(clicked()), scroll_area, SLOT(show()));
	connect(send_file,SIGNAL(clicked()), file_browser, SLOT(show()));
	connect(file_browser,SIGNAL(fileSelected(QString)), 
			this, SLOT(sending_file(QString)));
}

void mainWindow::eConnections()
{
	emit incomingConnection();
}


void mainWindow::e_file_request(int fd, char *file_name, int file_size)
{
	emit file_request(fd, file_name, file_size);
}

void mainWindow::chattingWindow(QListWidgetItem *item)
{
	contact *cnt = (contact*)item;
	connect(cnt->sendMess, SIGNAL(returnPressed()),cnt,SLOT(insertMess()));
	cnt->chtWin->show();
}

void mainWindow::addToList()
{ 
	contact *tmp;
	if(fd > 0)
	{
		tmp = new contact(h, remoteNick, fd, hostNick);
	       	fd = -2;
	}
	else
	{
		tmp = new contact(((h_name->text()).toStdString()), 
			remoteNick, fd, hostNick);
	}
	contacts->addItem( tmp );

}

void mainWindow::addContact()
{
	h_name = new QLineEdit;

	QWidget *dialog = new QWidget;
	QPushButton *ok = new QPushButton(tr("OK"));
	QPushButton *cancel = new QPushButton(tr("Cancel"));
	QLabel *hostName = new QLabel (tr("Host name:"));

	QHBoxLayout *layout1 =  new QHBoxLayout;
	layout1->addWidget(hostName);
	layout1->addWidget(h_name);

	QHBoxLayout *layout2 =  new QHBoxLayout;
	layout2->addWidget(ok);
	layout2->addWidget(cancel);

	QVBoxLayout *mainLayout =  new QVBoxLayout;
	mainLayout->addLayout(layout1);
	mainLayout->addLayout(layout2);

	connect(cancel,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(ok,SIGNAL(clicked()),this,SLOT(addToList()));
	connect(ok,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(h_name, SIGNAL(returnPressed()),ok,SIGNAL(clicked()));

	dialog->setLayout(mainLayout);
	dialog->show();
}

void mainWindow::recieve_file(int fd, char *file_name, int file_size)
{
	recieve_t *bar = new recieve_t(fd,file_name,file_size);
	progress_layout->insertWidget(0,bar);
	scroll_area->setWidget(scroll_widget);
	scroll_area->setWidgetResizable(true);
	scroll_area->show();
}

void mainWindow::response_file_request(int fd, char *file_name, int file_size)
{
	resp *r = new resp(fd, file_name, file_size);
	connect(r,SIGNAL(accept_file(int, char*,int)), 
				this,SLOT(recieve_file(int,char*,int)));
	
}

void mainWindow::sending_file(QString file_name)
{
	file_path = file_name;
	std::cout << file_path.toStdString() << std::cout;
	send_file_h = new QLineEdit;

	QLineEdit *insert_h_name = new QLineEdit;
	send_file_h = insert_h_name;

	QWidget *dialog = new QWidget;
	QPushButton *ok = new QPushButton(tr("Send"));
	QPushButton *cancel = new QPushButton(tr("Cancel"));
	QLabel *hostName = new QLabel (tr("Host name:"));

	QHBoxLayout *layout1 =  new QHBoxLayout;
	layout1->addWidget(hostName);
	layout1->addWidget(insert_h_name);

	QHBoxLayout *layout2 =  new QHBoxLayout;
	layout2->addWidget(ok);
	layout2->addWidget(cancel);

	QVBoxLayout *mainLayout =  new QVBoxLayout;
	mainLayout->addLayout(layout1);
	mainLayout->addLayout(layout2);
	connect(cancel,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(ok,SIGNAL(clicked()),dialog,SLOT(close()));
	connect(ok,SIGNAL(clicked()),this,SLOT(send_f()));

	dialog->setLayout(mainLayout);
	dialog->show();
}
void mainWindow::send_f()
{
	send_t *s = new send_t(file_path,send_file_h->text());
	progress_layout->insertWidget(0,s);
	scroll_area->setWidget(scroll_widget);
	scroll_area->setWidgetResizable(true);
	scroll_area->show();
}
